package com.liuxvru.project.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.liuxvru.common.constant.ProjectConstants;
import com.liuxvru.common.constant.ProjectRoleConstants;
import com.liuxvru.common.utils.SecurityUtils;
import com.liuxvru.project.domain.Project;
import com.liuxvru.project.domain.ProjectUserAssignment;
import com.liuxvru.project.mapper.ProjectMapper;
import com.liuxvru.project.mapper.ProjectUserAssignmentMapper;
import com.liuxvru.project.service.IProjectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 项目Service业务层处理
 *
 * @author liuxvru
 * @date 2024-11-22
 */
@Service
public class ProjectServiceImpl extends ServiceImpl<ProjectMapper, Project> implements IProjectService {

    @Autowired
    private ProjectUserAssignmentMapper projectUserAssignmentMapper;
    /**
     * 查询项目列表
     *
     * @param project 项目
     * @return 项目
     */
    @Override
    public List<Project> selectProjectList(Project project) {
        return baseMapper.selectProjectList(project);
    }

    @Override
    @Transactional
    public int createProject(Project projects) {
        Long userId = SecurityUtils.getUserId();
        projects.setStatus(ProjectConstants.PROJECT_INITIAL_STATUS);
        projects.setLeaderId(userId);
        projects.setCreatedBy(userId);
        projects.setUpdatedBy(userId);
        projects.setProgressPercentage(0L);
        baseMapper.insert(projects);
        ProjectUserAssignment projectUserAssignment = new ProjectUserAssignment();
        projectUserAssignment.setProjectId(projects.getProjectId());
        projectUserAssignment.setUserId(userId);
        projectUserAssignment.setUserName(SecurityUtils.getUsername());
        projectUserAssignment.setProjectRole(ProjectRoleConstants.PROJECT_INITIAL_PROJECT_MANAGER);
        projectUserAssignment.setCreatedBy(userId);
        projectUserAssignment.setUpdatedBy(userId);
        projectUserAssignmentMapper.insert(projectUserAssignment);
        return 1;
    }

    @Override
    public int edit(Project project) {
        Long userId = SecurityUtils.getUserId();
        project.setUpdatedBy(userId);
        return baseMapper.updateById(project);
    }

}
